package com.heima.behavior.controller.v1;

import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/behavior")
public class ApBehaviorController {

    @Autowired
    private IApLikesBehaviorService likesBehaviorService;

    @Autowired
    private IApUnlikesBehaviorService unlikesBehaviorService;

    /**
     * 查询用户点赞和不喜欢行为
     */
    @GetMapping("/findBehavior")
    public Map<String,Boolean> findBehavior(@RequestParam(name = "userId",required = false)Integer userId,
                                     @RequestParam(name = "equipmentId",required = false)String equipmentId,
                                     @RequestParam(name = "articleId")Long articleId,
                                     @RequestParam(name = "type")Integer likeType){
        //声明是否点赞和是否不喜欢变量
        Boolean isLike=false;
        Boolean isUnlike=false;
        //查询点赞行为
        ApLikesBehavior likesBehavior = likesBehaviorService.queryLikeBehavior(userId,equipmentId,articleId,likeType);
        if(likesBehavior!=null && likesBehavior.getOperation()==1){
            isLike=true;
        }
        //查询不喜欢行为
        ApUnlikesBehavior unlikesBehavior = unlikesBehaviorService.queryUnLikeBehavior(userId,equipmentId,articleId);
        if(unlikesBehavior!=null && unlikesBehavior.getType()==1){
            isUnlike=true;
        }
        Map<String,Boolean> map = new HashMap<>();
        map.put("islike",isLike);
        map.put("isunlike",isUnlike);
        return map;
    }
}
